<template>
	<view class="mypop">
		<u-popup :show="showPop" mode="center" height="500px" customStyle="width:90%" :round="10">
			<view class="p-3">
				<view class="" style='position: absolute;right: 40rpx;z-index: 10;'>
					<u-icon name="close" size="20" @click="refuse"></u-icon>
				</view>
				<view class="title" style="text-align: center;color: black;">
					{{title}}
				</view>
				<scroll-view scroll-y="true" style="height: 600rpx;">
					<view class="" style="margin-top: 15px;color: #979797;display: flex;margin-bottom: 20rpx;">
						<!-- <image src="../../static/my/write2.png" style="width: 20rpx;height: 20rpx;margin-right: 10rpx;"></image> -->
						<u-icon name="edit-pen-fill" customStyle="display:inline"></u-icon>{{tip}}
					</view>
					<u--textarea v-model="value" placeholder="请输入内容"></u--textarea>
					<view class="tsPerson">
						<view class="title">
							<u-icon name="man-add" size="22" color="#909399"></u-icon>添加推送人
						</view>
						<view class="login-user-compony ">
							<!-- 输入框 -->
							<input placeholder="检索推送人" v-model="comName" @focus="seachUserList" @input="seachUserList"
								placeholder-style="font-size:28rpx;line-height:34rpx;color:#B6BDBD" type="text" />
							<view class="checkedList" v-if="checkboxValue.length!=0" >
								<view class="item" v-for="(item,index) in checkBox" :key="index">
									<view class="name">
										{{item.realname}}
									</view>
									<view class="" @click="delItem(index)" style="margin-top: 2rpx;">
										<u-icon name="close" color="#292424" size="17"></u-icon>
									</view>
								</view>
							</view>
							<!-- 根据后端接口返回的 列表 -->
							<view class="login-user-List">
								<view class="u-page__checkbox-item">
									<u-checkbox-group v-model="checkboxValue" @change="checkboxChange"
										:borderBottom="true" placement="column" iconPlacement="right">
										<u-checkbox :customStyle="{marginBottom: '16px'}" activeColor="#47578E"
											v-for="(item, index) in userList" :key="item.id" :label="item.realname+'('+item.job_name+')'"
											:name="item.id">
										</u-checkbox>
									</u-checkbox-group>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="flex-r-a-s">
					<view style="width: 40%;">
						<u-button text="取消" type='info' @click='refuse'></u-button>
					</view>
					<view style="width: 40%;">
						<u-button class="custom-style" text="消息推送" customStyle="background-color: #47578E;color:#fff"
							@click="submit"></u-button>
					</view>
				</view>
			</view>
		</u-popup>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getUserList
	} from '@/request/api.js'
	export default {
		props: ['showPop', 'title', 'tip'],
		created() {
			this.getUserList()
		},
		watch: {
			showPop(value) {
				if ( value == false) {
					this.empt()
				}
			}
		},
		data() {
			return {
				show: false,
				value: '',
				comName: '',
				userList: [],
				checkboxValue: [],
				checkBox: []
			}
		},
		methods: {
			empt() {
				this.comName = ''
				this.userList = []
				this.checkboxValue = []
				this.checkBox = []
				this.value = ''
				this.getUserList()
			},
			delItem(e) {
				this.checkBox.splice(e, 1)
				this.checkboxValue = this.checkBox.map(item => {
					return item.id
				})
				console.log(this.checkBox, this.checkboxValue)
			},
			// 注意返回值为一个数组，单列时取数组的第一个元素即可(只有一个元素)
			submit(e) {
				// console.log(this.$refs.uToast,'uToast')
				this.$emit('submit', this.value,this.checkboxValue)
				this.$refs.uToast.show({
					message: '推送成功',
					type: 'success',
					duration: 2500
				})
				
			},
			refuse() {
				this.$emit('refuse', true)
			},
			seachUserList() {
				this.checkBox = []
				this.getUserList()
			},
			async getUserList() {
				let name = {
					search: this.comName
				}
				let res = await getUserList(name)
				this.userList = res.data
			
				if (this.userList.length == 0) {
					// this.formData.ibec_id = ''
				}
			},
			checkboxChange(n) {
				let selectList = n;
				this.checkBox = []
				// console.log(n, '选中的内容')
				this.userList.map(item => {
					selectList.forEach(res => {
						if (item.id == res) {
							this.checkBox.push(item)
						}
					})
				})
				// console.log(this.checkBox, '处理选择的内容')
			}
		}
	}
</script>

<style lang="scss">
	.checkedList {
		display: flex;
		flex-wrap: wrap;
		margin-top: 30rpx;
		display: flex;
		flex-wrap: wrap;
		border: .5px solid #dadbde;
		border-radius: 4px;
		padding: 9px;
		min-height: 94rpx;

		.item {
			display: flex;
			margin-right: 20rpx;
			margin: 20rpx 0;
			display: flex;
			margin-right: 20rpx;
			color: #292424;
			background: #04040412;
			padding: 10rpx;
			border-radius: 4px;

			.name {
				margin-right: 20rpx;
				font-size: 28rpx;
			}
		}
	}

	.login-user-compony {
		display: flex;
		border: .5px solid #dadbde;
		padding-top: 6px;
		padding-bottom: 6px;
		padding-left: 0px;
		padding-right: 0px;
		height: 24px;
		border-radius: 4px;
		justify-content: space-between;
		flex-direction: column;
		position: relative;
	}

	.login-user-compony input {
		padding-left: 20rpx;
	}

	.login-user-List {
		background-color: #FFFFFF;
		margin-top: 34rpx;
		// border: 2rpx solid #e8e2e2;
		// height: 45vh;
		// overflow: scroll;
		// position: absolute;
		// width: 100%;
		// top: 50px;

		&-child {
			border: 2rpx solid #f7f7f7;
			height: 80rpx;
			line-height: 80rpx;
			padding-left: 10rpx;
			color: #6f6464;
		}
	}

	::v-deep .u-icon {
		display: inline !important;
	}

	.tsPerson {
		.title {
			color: #979797;
			margin: 20rpx 0;
			display: flex;
		}
	}

	.mypop {
		width: 90%;
	}
</style>